package com.yanqu.road.server.cmd.activity.tongbing.cross;

import com.yanqu.road.entity.activity.tongbing.config.TongBingConfig;
import com.yanqu.road.entity.activity.tongbing.config.TongBingTitleConfig;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.enums.eTongBingPosition;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TongBingProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tongbing.TongBingModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.tongbing.TongBingMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.text.MessageFormat;
import java.util.Map;


@Cmd(code = GameProtocol.S_CROSS_TONG_BING_CITY_APPOINT_POSITION, desc = "")
public class TongBingAppointPotionCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        TongBingProto.CrossTongBingCityWinerAppointRespMsg reqMsg = TongBingProto.CrossTongBingCityWinerAppointRespMsg.parseFrom(packet.getBytes());
        long userId = packet.getPlayerId();
        if(reqMsg.getSendReward()){
            TongBingConfig config = TongBingMgr.getConfig();
            Map<Integer, TongBingTitleConfig> configMap = config.getTitleConfigMap();
            TongBingTitleConfig titleConfig = configMap.get(reqMsg.getPosition());
            if(!titleConfig.getRewards().equals("0") && !StringUtils.isNullOrEmpty(titleConfig.getRewards())){
                UserInfo userInfo = UserMgr.getUserInfo(userId);
                String activityName = ActivityMgr.getActivityName(config.getActivityInfo(), userInfo.getLanguage());
                String positionName = ServerLanguageMgr.getContent(titleConfig.getName(), userInfo.getLanguage());
                String title,content;
                if(titleConfig.getId() == eTongBingPosition.Master.getValue()){
                    title = ServerLanguageMgr.getContent(config.getMailFirstTitleReward(), userInfo.getLanguage());
                    content = StringUtils.format(ServerLanguageMgr.getContent(config.getMailFirstContentReward(), userInfo.getLanguage()),
                            activityName, positionName);
                }else{
                    title = ServerLanguageMgr.getContent(config.getMailWinTitleReward(), userInfo.getLanguage());
                    content = StringUtils.format(ServerLanguageMgr.getContent(config.getMailWinContentReward(), userInfo.getLanguage()),
                            activityName, reqMsg.getWinerName(),positionName);
                }
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), titleConfig.getRewards(), content, title, config.getActivityInfo().getActivityId(), config.getActivityInfo().getServerListStr());
            }
        }
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if(player != null){
            player.getModule(TongBingModule.class).winnerAppoint(reqMsg.getPosition());
            if(reqMsg.getSendReward()){
                player.getModule(TongBingModule.class).syncUserData();
            }
        }
    }

}
